import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/index',
        name: 'index',
        meta: {
          title: '系统首页'
        },
        component: () => import('../views/index.vue')
      },
      {
        path: '/department_tree',
        name: 'department_tree',
        meta: {
          title: '组织架构图'
        },
        component: () => import('../views/department/departmentTree.vue')
      },
      {
        path: '/department_list',
        name: 'department_list',
        meta: {
          title: '部门列表'
        },
        component: () => import('../views/department/departmentList.vue')
      },
      {
        path: '/staff_list',
        name: 'staff_list',
        meta: {
          title: '员工列表'
        },
        component: () => import('../views/staff/staffList.vue')
      },
      {
        path: '/staff_info_list',
        name: 'staff_info_list',
        meta: {
          title: '员工信息列表'
        },
        component: () => import('../views/staff/staffInfoList.vue')
      },
      {
        path: '/resign_list',
        name: 'resign_list',
        meta: {
          title: '辞职申请列表'
        },
        component: () => import('../views/staff/resignList.vue')
      },
      {
        path: '/table',
        name: 'basetable',
        meta: {
          title: '表格'
        },
        component: () => import('../views/BaseTable.vue')
      },
      {
        path: '/charts',
        name: 'basecharts',
        meta: {
          title: '图表'
        },
        component: () => import('../views/BaseCharts.vue')
      },
      {
        path: '/form',
        name: 'baseform',
        meta: {
          title: '表单'
        },
        component: () => import('../views/BaseForm.vue')
      },
      {
        path: '/tabs',
        name: 'tabs',
        meta: {
          title: 'tab标签'
        },
        component: () => import('../views/Tabs.vue')
      }, {
        path: '/donate',
        name: 'donate',
        meta: {
          title: '鼓励作者'
        },
        component: () => import(/* webpackChunkName: "donate" */ '../views/Donate.vue')
      },
      {
        path: '/permission',
        name: 'permission',
        meta: {
          title: '权限管理',
          permission: true
        },
        component: () => import('../views/Permission.vue')
      },
      {
        path: '/i18n',
        name: 'i18n',
        meta: {
          title: '国际化语言'
        },
        component: () => import('../views/I18n.vue')
      },
      {
        path: '/upload',
        name: 'upload',
        meta: {
          title: '上传插件'
        },
        component: () => import('../views/Upload.vue')
      },
      {
        path: '/icon',
        name: 'icon',
        meta: {
          title: '自定义图标'
        },
        component: () => import('../views/Icon.vue')
      },
      {
        path: '/404',
        name: '404',
        meta: {
          title: '找不到页面'
        },
        component: () => import('../views/404.vue')
      },
      {
        path: '/403',
        name: '403',
        meta: {
          title: '没有权限'
        },
        component: () => import('../views/403.vue')
      },
      {
        path: '/user',
        name: 'user',
        meta: {
          title: '个人中心'
        },
        component: () => import('../views/User.vue')
      },
      {
        path: '/editor',
        name: 'editor',
        meta: {
          title: '富文本编辑器'
        },
        component: () => import('../views/Editor.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录'
    },
    component: () => import('../views/Login.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  document.title = 'alisleepy后台管理'
  const role = localStorage.getItem('ali_name')
  if (!role && to.path !== '/login') {
    console.log('无权限，请登录')
    next('/login')
  } else if (to.meta.permission) {
    // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
    // role === 'admin'
    //     ? next()
    //     : next('/403');
    next()
  } else {
    next()
  }
})

export default router
